<template>
    <div class="login-container">
        <div class="login-box">
            <div class="title-container">
                <h3 class="title">登 录 BK-SOPS(Ver.OPEN)</h3>
            </div>
            <div class="username-container">
                <bk-input
                    name="username"
                    :maxlength="255"
                    :placeholder="'输入用户名'"
                    :clearable="true"
                    :show-word-limit="true"
                    :right-icon="'bk-icon'"
                    v-model="loginForm.username"
                    v-validate="userNameRule"
                >
                </bk-input>
                <span v-show="veeErrors.has('username')">{{ veeErrors.first('username') }}</span>
            </div>
            <div class="userpass-container">
                <bk-input
                    name="password"
                    :placeholder="'输入密码'"
                    :type="'password'"
                    :clearable="true"
                    v-model="loginForm.password"
                    v-validate="passWordRule"
                ></bk-input>
                <span v-show="veeErrors.has('password')">{{ veeErrors.first('password') }}</span>
            
            </div>
            <div class="login-btn">
                <bk-button theme="primary"
                    :loading="isLoading"
                    @click="handleLogin"
                >登录</bk-button
                >
                <!-- <bk-button theme="primary" size="large" :loading="true" class="mr10">loading</bk-button> -->
            </div>
        </div>

        <!-- <el-form class="login-form" autocomplete="on" label-position="left">
            <div class="title-container">
                <h3 class="title">登录</h3>
            </div>
            <el-form-item>
                <el-input
                    placeholder="用户名..."
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="on"
                />
                <el-tooltip content="Caps lock is On" placement="right" manual>
                    <el-form-item prop="password">
                        <el-input
                            placeholder="Password"
                            name="password"
                            tabindex="2"
                            autocomplete="on"
                            @keyup.native="checkCapslock"
                            @blur="capsTooltip = false"
                            @keyup.enter.native="handleLogin"
                        />
                    </el-form-item>
                </el-tooltip>
                <el-button
                    type="primary"
                    style="width: 100%; margin-bottom: 30px"
                    @click.native.prevent="handleLogin"
                >登录</el-button>
            </el-form-item>

        </el-form> -->
    </div>
</template>
<script>
    import { bkInput } from 'bk-magic-vue'
    import { STRING_LENGTH } from '@/constants/index.js'
    import { mapActions, mapMutations } from 'vuex'

    export default {
        components: {
            bkInput
        },
        data () {
            return {
                loginForm: {
                    username: 'admin',
                    password: 'admin'
                },
                userNameRule: {
                    required: true,
                    max: STRING_LENGTH.USERNAME_MAX_LENGTH
                    // regex: NAME_REG
                },
                passWordRule: {
                    required: true,
                    max: STRING_LENGTH.PASSWORD_MAX_LENGTH
                },
                passwordType: 'password',
                capsTooltip: false,
                isLoading: false,
                showDialog: false,
                redirect: undefined,
                otherQuery: {}
            }
        },
        watch: {},
        created () {},
        mounted () {
            if (this.loginForm.username === '') {
                this.$refs.username.focus()
            } else if (this.loginForm.password === '') {
                this.$refs.password.focus()
            }
        },
        destroyed () { },
        methods: {
            ...mapMutations([
                'setUserName'

            ]),
            ...mapActions('user', [
                'login'
            ]),
            handleLogin () {
                this.isLoading = true
                this.$validator.validateAll().then((valid) => {
                    // 验证无问题,valid为true，console.log(valid)
                    if (!valid) {
                        this.isLoading = false
                        console.log('valid error in form')
                    } else {
                        this.isLoading = true
                        this.login(this.loginForm).then((resp) => {
                            // console.log(resp)
                            if (resp.result) {
                                this.setUserName(resp.data.username)
                                this.$router.push({ path: '/home/' })
                            } else {
                                this.isLoading = false
                            }
                        }).catch((error) => {
                            console.log(error)
                        })
                        // console.log(resp)
                        // this.$store.dispatch('user/login', this.loginForm)
                        //     .then(() => {
                        //         this.$router.push('/home')
                        //         this.loading = false
                        //     })
                        //     .catch(() => {
                        //         this.loading = false
                        //     })
                    }
                }).finally(() => {
                    this.isLoading = false
                })
                // this.$router.push({ path: '/dashboard' })
            }
        }
            
    }
</script>
<style>
/* 覆盖父级class中高度 */
.main-container {
    height: calc(100vh - 1px) !important;
}
</style>
<style lang="scss" scoped>
.login-box {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    //background-color:#eee;
}
.username-container {
    padding: 0 0 20px;
}
.userpass-container {
    padding: 0 0 20px;
}
.bk-button.bk-primary {

    width: 100%;
    font-size: 14px;
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

}
</style>
